package kickboxing.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kickboxing.controller.FTPManager;
import kickboxing.controller.UzytkownikManager;
import kickboxing.model.Uzytkownik;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

public class ZdjecieProfiloweAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ZdjecieProfiloweAction() {
        super();
    }

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesja = request.getSession(true);
		Uzytkownik user = (Uzytkownik) sesja.getAttribute("user");
		FTPManager ftp = new FTPManager();
		
		if(user != null){
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	        int i=0;
	        if (isMultipart) {
	            FileItemFactory factory = new DiskFileItemFactory();
	            ServletFileUpload upload = new ServletFileUpload(factory);
	            
	            try {
	            	ftp.polaczZFTP();
	                List items = upload.parseRequest(request);
	                Iterator iterator = items.iterator();
	                while (iterator.hasNext()) {
	                    FileItem item = (FileItem) iterator.next();
	                    
	                    if (!item.isFormField()) {
	                        String fileName = item.getName();
	 
	                        String root = getServletContext().getRealPath("/");
	                        File path = new File(root + "/uploads");
	                        if (!path.exists()) {
	                            boolean status = path.mkdirs();
	                        }
	                        File uploadedFile = new File(path + "/" + fileName);
	                        System.out.println(uploadedFile.getAbsolutePath());
	                       
	                        item.write(uploadedFile);
	                        ftp.wybierzKatalogFTP("gladiator/zdjeciaProfilowe/");
	                        String nazwaPliku = user.getLogin()+"Profilowe.jpg";
	                        ftp.wrzucNaSerwer(uploadedFile, nazwaPliku);
	                        ftp.rozlaczZFTP();
	                        String message = "Zmieniono zdjecie profilowe!";
	            			request.setAttribute("message", message);
	            			String x = "?message="+message;
	            			response.sendRedirect("index.jsp"+x);
	                    }
	                }
	            } catch (FileUploadException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		}else{
			response.sendError(404, "Brak Uprawinien");
		}
		
		
		
	}
		
		
	

}
